package org.tgcloud.group.core.mapUtils;

/**
 * @Author: zhy
 * @Created: 2025-02-28-上午10:35
 * @Description:
 */
public class DistanceCalculator {

    private static final double EARTH_RADIUS_KM = 6371.0;

    public static double calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double lat1Rad = Math.toRadians(lat1);
        double lon1Rad = Math.toRadians(lon1);
        double lat2Rad = Math.toRadians(lat2);
        double lon2Rad = Math.toRadians(lon2);

        double deltaLat = lat2Rad - lat1Rad;
        double deltaLon = lon2Rad - lon1Rad;

        double a = Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.pow(Math.sin(deltaLon / 2), 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        return EARTH_RADIUS_KM * c; // 返回的是两点间的距离，单位是公里
    }

    public static Integer getRadiusKm(double neLat, double neLng, double swLat, double swLng) {

        // 计算中心点
        double centerLat = (neLat + swLat) / 2;
        double centerLng = (neLng + swLng) / 2;

        // 计算中心点到东北角的距离
        double distanceToNE = calculateDistance(centerLat, centerLng, neLat, neLng);

        // 或者计算中心点到西南角的距离
        double distanceToSW = calculateDistance(centerLat, centerLng, swLat, swLng);

        // 假设地图是正方形，选择较大的距离作为半径
        double radius = Math.max(distanceToNE, distanceToSW);
        System.out.println("Radius of the map view: " + radius + " km");
        return (int) Math.round(radius);
    }

}
